package com.qiche.controller.mgr;

import com.qiche.aspect.Result;
import com.qiche.model.Product;
import com.qiche.model.User;
import com.qiche.model.Workstage;
import com.qiche.service.WorkstageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品工序管理控制器
 * Created by Linzw on 2018/01/24.
 */
@Api(value = "/workstages",description = "产品工序管理控制器")
@RestController
@RequestMapping("/workstages")
public class WorkstageController {

    @Autowired
    private WorkstageService workstageService;

    @ApiOperation(value ="根据产品ID获取工序列表" )
    @GetMapping(value = "/getListByProductId/{id}")
    public Result getListByProductId(@PathVariable("id") Long itemId) {
        List<Workstage> workstageList=workstageService.selectByItemId(itemId);
       return Result.createBySuccess(workstageList);
    }


    @ApiOperation(value ="添加工序" )
    @PostMapping
    public Result add(@RequestBody Workstage workstage) {
        int saveCount=workstageService.save(workstage);
        if (saveCount>0) {
            return Result.createBySuccessMessage("添加成功");
        }
        return Result.createByErrorMessage("添加失败");
    }

    @ApiOperation(value ="编辑工序" )
    @PutMapping
    public Result update(@RequestBody Workstage workstage) {
        int updateCount = workstageService.updateNotNull(workstage);
        if (updateCount>0) {
            return Result.createBySuccessMessage("更新成功");
        }
        return Result.createByErrorMessage("更新失败");
    }

    @ApiOperation(value ="根据ID删除工序" )
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@PathVariable("id") Long workstageId) {
        int deleteCount = workstageService.delete(workstageId);
        if (deleteCount>0) {
            return Result.createBySuccessMessage("删除成功");
        }
        return Result.createByErrorMessage("删除失败");
    }
}
